package com.etl.controller;


import com.etl.cache.TokenCache;
import com.etl.constants.CommonConstant;
import com.etl.entity.EtlUser;
import com.etl.model.rev.LoginUser;
import com.etl.model.rev.LoginUserQuery;
import com.etl.service.IEtlUserService;
import com.etl.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Slf4j
@RestController
@Api(tags = "登录管理", value = "登录退出相关接口")
public class LoginController {

    @Value("${cookie.domain}")
    private String domain;

    @Value("${cookie.maxAge}")
    private Integer maxAge;

    @Resource
    private TokenCache tokenCache;

    @Resource
    private IEtlUserService IEtlUserService;


    @PutMapping("/login")
    @ApiOperation(value = "登录")
    public ResultVO login(HttpServletResponse response, @Validated @RequestBody LoginUserQuery loginUserQuery,
                          BindingResult bindingResult) {

        //校验参数是否为空
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            log.error("更新鸿信待办,错误参数" + errorList.get(0).getDefaultMessage());
            return ResponseUtil.error(0, errorList.get(0).getDefaultMessage());
        }



        EtlUser etlUser = new EtlUser();
        etlUser.setAccount(loginUserQuery.getAccount());

        String password = null;
        try {
            password = MD5Util.getMD5(loginUserQuery.getPassword());
        } catch (MD5Util.MD5Exception e) {
            e.printStackTrace();
        }
        etlUser.setPassword(password);
        List<EtlUser> etlUserList = IEtlUserService.getEtlUser(etlUser);
        if(etlUserList ==null || etlUserList.size() == 0){
            return ResponseUtil.error(0,"账号或者密码不正确！");
        }else {


            String tokenKey = AESUtil.encrypt(String.format("%s-%s", loginUserQuery.getAccount(), DateUtil.getCurrentDate()), AESUtil.password);
            LoginUser loginUser = new LoginUser();
            loginUser.setAccountName(etlUserList.get(0).getAccount());
            loginUser.setChsName(etlUserList.get(0).getFullName());
            loginUser.setUserType(etlUserList.get(0).getUserType());
            tokenCache.setToken(tokenKey, loginUser);
            //设置cookie
            CookieUtil.set(response, CommonConstant.COOKIE_KEY, tokenKey, maxAge, domain);


        }
        return ResponseUtil.success();


    }

    @GetMapping("/loginOut")
    @ApiOperation(value = "退出登录")
    public ResultVO loginOut(HttpServletRequest request,HttpServletResponse response) {
//        String token = request.getHeader("");
//        tokenCache.delToken(token);
        //清除cookie
        CookieUtil.set(response, CommonConstant.COOKIE_KEY, null, 0, domain);
        return ResponseUtil.success();


    }

}
